/* $RCSFile: ADNDynLead.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynLead.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:49  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using CrmSdk;
using AddonNice.CRMWrapper;

namespace AddonNice.CRMWrapper
{
    public class ADNDynLead : ADNDynCustomer
    {
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.lead)
            {}

            public override Type DynType
            { get { return typeof(ADNDynLead); } }

            public override string DynTypename
            {   get{ return CrmStr.lead;}}

            public override DynEntity Build()
            {   return new ADNDynLead();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynLead(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynLead(w,PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynLead(w,PrimaryKeyValue);}
        }

        static ADNDynLead(){new Ftor();} 

        /// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynLead(): base()
		{}

        /// <summary>
        /// Ctor for querying crm to find corresponding account
        /// </summary>
        public ADNDynLead(CRMWrapperBase w, Guid CrmId)
            : base(w, CrmStr.lead, CrmId)
        {}

        /// <summary>
        /// To use when you build a lead name is the name for the lead, any other value must be filled before create
        /// </summary>
        public ADNDynLead(CRMWrapperBase w,string wlastname)
            : base(w,CrmStr.lead)
        {
            lastname    =   wlastname;
        }

        /// <summary>
        /// To use when you build a lead name is the name for the lead, any other value must be filled before create
        /// </summary>
        public ADNDynLead(CRMWrapperBase w, string wfirstname,string wlastname)
            : base(w,CrmStr.lead)
        {
            firstname   =   wfirstname;
            lastname    =   wlastname;
        }

        /// <summary>
        /// To use when you have retrieved a dynamic entity and want to map it 
        /// </summary>
        public ADNDynLead(CRMWrapperBase w, InternalDynamicEntity dEntity)
            : base(w,CrmStr.lead,dEntity, null, null)
        {}

        public ADNDynLead(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection prop, InternalColumnSet s)
            : base(w,CrmStr.lead,dEntity, prop, s)
        {}

        /// <summary>
        /// Ctor for querying crm to find corresponding account
        /// </summary>
        public ADNDynLead(ResetWrapperHandler fn, Guid CrmId)
            : base(fn, CrmStr.lead, CrmId)
        {}

        /// <summary>
        /// To use when you build a lead name is the name for the lead, any other value must be filled before create
        /// </summary>
        public ADNDynLead(ResetWrapperHandler fn, string wfirstname,string wlastname)
            : base(fn,CrmStr.lead)
        {
            firstname   =   wfirstname;
            lastname    =   wlastname;
        }

        /// <summary>
        /// To use when you have retrieved a dynamic entity and want to map it 
        /// </summary>
        public ADNDynLead(ResetWrapperHandler fn, InternalDynamicEntity dEntity)
            : base(fn,CrmStr.lead,dEntity, null, null)
        {}

        public ADNDynLead(ResetWrapperHandler fn, InternalDynamicEntity dEntity, PropertyDescriptorCollection prop, InternalColumnSet s)
            : base(fn,CrmStr.lead,dEntity, prop, s)
        {}

        [CrmDynAttribute(CrmStr.lead)]
        public Guid leadid
        {
            get
            {
                return KeyPropertyValue(CrmStr.leadid);
            }
            set
            {
                AddKeyProperty(CrmStr.leadid, value);
            }
        }

        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.leadid) || KeyPropertyValue(CrmStr.leadid) != crmId )
                leadid  =   crmId;
        }

        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return leadid;
           }
           set
           {
               leadid    =   value;
           }
        }

        public override string PrimaryFieldName
        {
            get
            {
               return CrmStr.fullname;
            }
           set
           {}
        }

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return fullname;
            }
            set
            {
                fullname =   value;
            }
        }

        [CrmDynAttribute(CrmStr.lead)]
        public string firstname
        {
            get
            {
                return StringPropertyValue(CrmStr.firstname);
            }
            set
            {
                AddStringProperty(CrmStr.firstname, value);
            }
        }

        [CrmDynAttribute(CrmStr.lead)]
        public string lastname
        {
            get
            {
                return StringPropertyValue(CrmStr.lastname);
            }
            set
            {
                AddStringProperty(CrmStr.lastname, value);
            }
        }

 
        #region customer

        string _DefaultCustomerName   =   CrmStr.account;
		public string DefaultCustomerName
		{
			get
			{
				return _DefaultCustomerName;
			}
			set
			{
                switch (value)
                {
                    case CrmStr.account:
                    case CrmStr.contact:
                        _DefaultCustomerName  =   value;
                        break;
                }
			}
		}

        /// <summary>
        /// Beware : set defaults to account, to change it set DefaultCustomerName property before calling set
        /// or use SetContactCustomerid for adding contacts
        /// </summary>
        [CrmDynAttribute(CrmStr.lead)]
        public Guid customerid 
        {
            get 
            {
                return CustomerPropertyValue(CrmStr.customerid);
            }
            set 
            {
                AddCustomerProperty(CrmStr.customerid,DefaultCustomerName,value);
            }
        }

        /// <summary>
        /// The entity : account or contact
        /// </summary>
        public string customerType
        {
            get 
            {
                return CustomerPropertyType(CrmStr.customerid);
            }
        }

         /// <summary>
        /// To adding contact customerids
        /// </summary>
        public void SetContactCustomerid(Guid contactid)
        {
                AddCustomerProperty(CrmStr.customerid,CrmStr.contact,contactid);
        }

        #endregion customer
 
        [CrmDynAttribute(CrmStr.lead)]
        public string companyname
        {
            get
            {
                return StringPropertyValue(CrmStr.companyname);
            }
            set
            {
                AddStringProperty(CrmStr.companyname, value);
            }
        }

        [CrmDynAttribute(CrmStr.lead)]
        public string jobtitle
        {
            get
            {
                return StringPropertyValue(CrmStr.jobtitle);
            }
            set
            {
                AddStringProperty(CrmStr.jobtitle, value);
            }
        }

        [CrmDynAttribute(CrmStr.lead)]
        public string subject
        {
            get
            {
                return StringPropertyValue(CrmStr.subject);
            }
            set
            {
                AddStringProperty(CrmStr.subject, value);
            }
        }

        [CrmDynAttribute(CrmStr.lead)]
        public string salutation
        {
            get
            {
                return StringPropertyValue(CrmStr.salutation);
            }
            set
            {
                AddStringProperty(CrmStr.salutation, value);
            }
        }

        [CrmDynAttribute(CrmStr.lead)]
        public string mobilephone
        {
            get
            {
                return StringPropertyValue(CrmStr.mobilephone);
            }
            set
            {
                AddStringProperty(CrmStr.mobilephone, value);
            }
        }
        
        [CrmDynAttribute(CrmStr.lead)]
        public int numberofemployees
        {
            get
            {
                if ( ContainsKey(CrmStr.numberofemployees) )
                    return NumberPropertyValue(CrmStr.numberofemployees);
                return 0;
            }
            set
            {
                AddNumberProperty(CrmStr.numberofemployees,value);
            }
        }

        [CrmDynAttribute(CrmStr.lead)]
        public Guid campaignid 
        {
			get
			{
				return LookupPropertyValue(CrmStr.adncampaignid);
			}
			set
			{
				AddLookupProperty(CrmStr.adncampaignid,CrmStr.campaign, value);
			}
        }

        public string campaignName
        {
			get
			{
				return LookupPropertyName(CrmStr.adncampaignid);
			}
        }
        
        
        /// <summary>
        /// 1 Advertisement
        /// 2 Employee Referral
        /// 3 External Referral
        /// 4 Partner
        /// 5 Public Relations
        /// 6 Seminar
        /// 7 Trade Show
        /// 8 Web 
        /// 9 Word of Mouth 
        /// Internet Marketing (for internal use only)
        /// 10 Other
        /// </summary>
        [CrmDynAttribute(CrmStr.lead)]
        public int leadsourcecode
		{
			get
			{
				return PicklistPropertyIntValue(CrmStr.leadsourcecode);
			}
			set
			{
				AddPicklistProperty(CrmStr.leadsourcecode,value);
			}
		}
       
        public string leadsourcecodeLabel
        {
            get
            {
                if ( ContainsKey(CrmStr.leadsourcecode) )
                    return PicklistPropertyLabelValue(CrmStr.leadsourcecode);
                return string.Empty;
            }
        }

        /// <summary>
        /// 1 Hot
        /// 2 Warm
        /// 3 Cold
        /// </summary>
        [CrmDynAttribute(CrmStr.lead)]
        public int leadqualitycode
		{
			get
			{
				return PicklistPropertyIntValue(CrmStr.leadqualitycode);
			}
			set
			{
				AddPicklistProperty(CrmStr.leadqualitycode,value);
			}
		}
       
        public string leadqualitycodeLabel
        {
            get
            {
                if ( ContainsKey(CrmStr.leadqualitycode) )
                    return PicklistPropertyLabelValue(CrmStr.leadqualitycode);
                return string.Empty;
            }
        }
        /// <summary>
        /// virtual because on 'detail' records it is only a uniqueidentifier property
        /// </summary>
        [CrmDynAttribute(CrmStr.lead)]
        public Guid owningbusinessunit
        {
            get
            {
                return LookupPropertyValue(CrmStr.owningbusinessunit);
            }
            set
            {
                AddLookupProperty(CrmStr.owningbusinessunit,CrmStr.businessunit,value);
            }
        }
        
        /// <summary>
        /// Owner for this object
        /// </summary>
        [CrmDynAttribute(CrmStr.lead)]
        public override Guid ownerid
        {
            get
            {
                if ( ContainsKey(CrmStr.ownerid) )
                    return OwnerPropertyValue(CrmStr.ownerid);
                return Guid.Empty;
            }
            set
            {
                AddOwnerProperty(CrmStr.ownerid,CrmStr.systemuser,value);
            }
        }

        public enum LeadState : int
        {
            Open            =   0,
            Qualified       =   1,
            Disqualified    =   2
        }

        public enum OpenLead  : int // Specifies that the lead is open. 
        {
            New             =   1,
            Contacted       =   2
        }

        public enum QualifiedLead  : int // Specifies that the lead is qualified. 
        {
            Qualified       =   3
        }

        public enum DisqualifiedLead  : int // Specifies that the lead is disqualified. 
        {
            Lost                =   4,
            CannotContact       =   5,
            NoLongerInterested  =   6,
            Canceled            =   7
        }
        
        public void SetState(LeadState st,int status,bool isAdmin)
        {
            wrapper.Util.SetLeadState(this,st,status,isAdmin);
        }
    
        #region Utilities

        public static ADNDynLead FindLeadByName(CRMWrapperBase Wrapper,ADNDynLead wdyn2,bool CreateIfNotFound,ref bool created,DynEntityCopyDelegate fn)
        {
            bool created2       =   created;
            ADNDynLead dynC     =   Wrapper.Util.FindLeadByName(wdyn2, CreateIfNotFound,ref created2, fn);
            created             =   created2;
            return dynC;
        }

        #endregion Utilities
    }
}
